package com.tianqu.tms.www.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.user.controller.UserUDTopListController;
import com.tianqu.tms.www.user.viewhelper.UserTopListViewhelper;

/**
 * 用户添加修改首页
 * 
 * @author tianshifu
 *
 */

public class UserUDTopListAction extends TmsAction {
		
	private Logger logger = Logger.getLogger(UserUDTopListAction.class);
	
	private static final String USERLIST = "userList";
	
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#doAction was called.");
		UserTopListViewhelper view=new UserTopListViewhelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			uc.setAttribute(SORTKEY, "4");
			uc.setAttribute(MAXINDEX, view.getMaxIndex());
			uc.setAttribute(ASC, "false");
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		
		UserUDTopListController controller = new UserUDTopListController();
		controller.execute(uc);
		
		view.setUserTopList((List) uc.getAttribute(USERLIST));//将获得的数据放到wiewhelp中
		User user = (User)uc.getAttribute(UserUDTopListController.PAGING);
		// viewhelp中设置翻页。
		view.setPageNo(Integer.toString(user.getPageNo()));
		view.setMaxIndex(Integer.toString(user.getMaxIndex()));
		view.setAsc(Boolean.toString(user.isAsc()));
		view.setSortKey(Integer.toString(user.getSortKey()));
		view.setTotalCount(Integer.toString(user.getTotalCount()));
		view.setDateFrom(Integer.toString(user.getDateFrom()));
		view.setDateTo(Integer.toString(user.getDateTo()));
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
